﻿seajs.config({
    base: '../js/',
    alias: {
        'labjs': 'lib/LAB.min',
    }
});

define(function (require) {
    require('labjs');
    // 载入基础lib
    $LAB.script('../js/lib/jquery-1.6.1.min.js').wait()
        .script('../js/lib/underscore-min.js').wait()
        .script('../js/lib/backbone.min.js').wait(
            function () {
                var service = require('utility/service');
                
                var LoginView = require('views/login.view');
                var AppView = require('views/app.view');
                var MessageView =require('views/message.view');
                var WorkitemView =require('views/workitemcontainer.view');
                
                var views = {
                    longinView: null,
                    appView: null,
                    messageView: null,
                    workitemView: null
                }

                function renderLoginView(){
                    views.longinView = new LoginView();
                    views.longinView.render();
                }                

                function renderAppView() {
                    views.appView = new AppView();
                    views.appView.render();
                }
                   
                function renderMessageView(){
                    views.messageView = new MessageView();
                    views.messageView.render();
                }

                function renderWorkitemView(project){
                    views.workitemView = new WorkitemView({project: project});
                    views.workitemView.render();
                }

                function eventsBinding(){
                    EventProxy.bind(EventProxy.Events.loginSuccess, function(){
                        renderAppView();
                        $.mobile.changePage('#feed-page',{
                            transition: "none"
                            , changeHash: false
                        }); 
                    }); 

                    EventProxy.bind(EventProxy.Events.writeMessage,function () {
                        renderMessageView(); 
                        
                        var page = $('#message-page').find('.ui-content').hide();
                        $.mobile.changePage('#message-page',{
                            transition: "none"
                            ,changeHash: false
                        });
                        
                        $('#message-page').page('destroy').page();
                        setTimeout(function(){
                            page.fadeIn();
                        },100); 
                    });  
                    EventProxy.bind(EventProxy.Events.goToPage,function (pageId,reverse) {
                        var page = $(pageId).find('.ui-content').hide();

                        $.mobile.changePage(pageId, {
                            transition: "none"
                            ,reverse: reverse?reverse:false
                            ,changeHash: false
                        });

                        setTimeout(function(){
                            page.fadeIn();
                        },100); 
                    }); 

                    EventProxy.bind(EventProxy.Events.finishChoose,function (members) {
                        var page = $('#message-page').find('.ui-content').hide();

                        $.mobile.changePage('#message-page', {
                                transition: "none"
                                ,reverse: true
                                ,changeHash: false
                        });

                        setTimeout(function(){
                            page.fadeIn();
                        },100); 

                        //todo:放到view里去做
                        views.messageView.addMembers(members);
                    });  
                
                    EventProxy.bind(EventProxy.Events.showProjectInfo,function(project){
                        renderWorkitemView(project);

                        var page = $('#workitem-page').find('.ui-content').hide();
                        $.mobile.changePage('#workitem-page',{
                            transition: "none"
                            ,reverse: true
                            ,changeHash: false});
                        
                        $('#workitem-page').page('destroy').page();

                        setTimeout(function(){
                            page.fadeIn();
                        },100); 
                    });   
                }

                $(function(){
                    renderLoginView();
                    eventsBinding();

//                    //Auto login for test
//                    service.Login(function(e){
//                        service.setAuthToken(e.Data.Token);
//                        if(e.Status == "Success"){
//                            EventProxy.trigger(EventProxy.Events.loginSuccess, e);
//                        }
//                    },{userName:  "hg", password: "123456"});
                });
            }
        )
        .script('../js/lib/jquery.mobile-1.0b2.js');
});
